package com.ledim.widget.viewPager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.ledim.adapter.base.LoopPagerAdapter;
import com.ledim.utils.LogUtil;

/**
 * 循环viewPager
 *
 * @author ccx
 */
public class LoopViewPager extends LetvBaseViewPager {

    protected OnPageChangeListener mOnPageChangeListener;
    protected int mPosition;
    private final int VIEW_MOVE_TAG = 1;
    /**
     * 轮播间隔 秒
     */
    private final int DELAY_MILLIS = 5 * 1000;

    //当前轮播图是否在滚动
    private boolean mIsStartMove = false;

    public LoopViewPager(Context context) {
        this(context, null);
    }

    public LoopViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {

                if (mOnPageChangeListener == null) {
                    return;
                }

                if (getAdapter() instanceof LoopPagerAdapter) {
                    LoopPagerAdapter infAdapter = (LoopPagerAdapter) getAdapter();
                    position = position % infAdapter.getRealCount();
                }

                if (mOnPageChangeListener != null) {
                    mOnPageChangeListener.onPageSelected(position);
                    mPosition = position;
                }

            }

            @Override
            public void onPageScrolled(int position, float arg1, int arg2) {
                if (mOnPageChangeListener != null) {
                    mOnPageChangeListener.onPageScrolled(position, arg1, arg2);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (mOnPageChangeListener != null) {
                    mOnPageChangeListener.onPageScrollStateChanged(state);
                }
            }
        });
    }

    /**
     * 如果是循环viewPager并且需要得到滑屏监听,那么需要执行这个方法
     *
     * @param listener
     */
    public void setLoopOnPageChangeListener(OnPageChangeListener listener) {
        mOnPageChangeListener = listener;
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        super.setAdapter(adapter);
        setCurrentItem(0);
    }

    /**
     * 跳转到下一个
     */
    @Override
    public void next() {
        if (getAdapter() != null && getAdapter().getCount() > 0) {
            int next = super.getCurrentItem() + 1;
            super.setCurrentItem(next, true);
        }
    }

    @Override
    public void setCurrentItem(int item) {
        setCurrentItem(item, false);
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        if (getAdapter() != null && getAdapter().getCount() > 0) {
            item = getOffsetAmount() + (item % getAdapter().getCount());
        }
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public int getCurrentItem() {
        int position = super.getCurrentItem();
        if (getAdapter() instanceof LoopPagerAdapter) {
            LoopPagerAdapter infAdapter = (LoopPagerAdapter) getAdapter();
            return (position % infAdapter.getRealCount());
        } else {
            return super.getCurrentItem();
        }
    }

    private int getOffsetAmount() {
        if (getAdapter() instanceof LoopPagerAdapter) {
            LoopPagerAdapter infAdapter = (LoopPagerAdapter) getAdapter();
            return infAdapter.getRealCount() * 100;
        } else {
            return 0;
        }
    }

    private android.os.Handler mHandler = new android.os.Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case VIEW_MOVE_TAG:
                    next();
                    //实现轮播
                    sendEmptyMessageDelayed(VIEW_MOVE_TAG, DELAY_MILLIS);
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 开始轮播
     */
    public void startMove() {
        LogUtil.e("ccx loopviewpage startMove");
        if (!mIsStartMove) {
            mIsStartMove = true;
            mHandler.sendEmptyMessageDelayed(VIEW_MOVE_TAG, DELAY_MILLIS);
        }

    }

    /**
     * 停止轮播
     */
    public void stopMove() {
        LogUtil.e("ccx loopviewpage stop");
        mIsStartMove = false;
        mHandler.removeMessages(VIEW_MOVE_TAG);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        doTouchEvent(ev);
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        doTouchEvent(ev);
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        doTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 触摸事件处理
     *
     * @param ev
     */
    private void doTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                stopMove();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                startMove();
                break;
        }
    }

    public void destroy() {
        if (mHandler != null) {
            mHandler.removeMessages(VIEW_MOVE_TAG);
        }
        this.removeAllViews();
    }

}
